<template>
    <!-- #ifdef MP-WEIXIN -->
    <view>
		<u-alert :title="title"  type = "warning" :closable="closable" :description = "description" v-if="alert==true"></u-alert>
        <view>
			<view class="content">
				<image class="logo" src="/static/bbdc.png"></image>
				<view class="text-area">
					<text class="title">邦邦打车</text>
				</view>
			</view>
            <!-- 有授权窗口 -->    
            <button  type="primary" class='btn' v-if="button==0" @click="getUserProfile">授权获取用户信息</button>
			<view style="text-align: center;color: cornflowerblue;margin-top: 50%;" @click="register">
				没有账号？前去注册
			</view>
        </view>
    </view>
    <!-- #endif -->
</template>
<script>
   export default {
	   // 获取code发送到后端获取openid
   		onLoad() {
   			uni.login({
   				provider: 'zh_CN',
   				success: function(res) {
   					// console.log(res, 24);
   					let code = res.code
   					// console.log(code, 26)
   					uni.request({
   						url:'http://localhost:8000/getOpenId',
						method:'GET',
   						data: {
   							code: code
   						},
   						success: function(res) {
   							// console.log(res.data, 33);
   							uni.setStorageSync("openid", res.data)
   						}
   					});
   				},
   			})
   		},
   		data() {
   			return {
   				title:'登录失败',
				description:'',
				closable:true,
				alert:false,
				button:0
   			}
   		},
   		methods: {
			register(){
				uni.navigateTo({
				  url: '../DriverRegister/DriverRegister'
				})
			},
			hideLoading(){
				uni.hideLoading();
			},
   			//授权登录弹窗
   			getUserProfile() {
				var that = this
				this.button=1
   				uni.getUserProfile({
   					desc: '授权登录',
   					success: (res) => {
						this.button=0
						
   						// 用户信息
   						let userInfo = res.userInfo
						let openid = uni.getStorageSync('openid')
   			// 			console.log(openid);
						// console.log(userInfo.avatarUrl,userInfo.nickName)
						uni.request({
							url: 'http://localhost:8000/DriverUser',
							method:'GET',
							data: {
								openid:openid,
							},
							success: function(res) {
								console.log(res, 64);
								if(res.data.result == 0){
									uni.showModal({
										title: '首次登录需输入已注册手机号',
										content: '',
										placeholderText: '请输入手机号',
										editable: true,
										confirm:false,
										success: function(res) {
											// console.log(res, 72)
											if (res.confirm) {
												uni.showLoading({
													title: '加载中'
												});
												let userTel = res.content
												// console.log(openid, 77)
												// console.log(res.content, 75);
												let reg =/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0|1|2|3|5|6|7|8|9])\d{8}$/
												if (userTel == '') {
													uni.hideLoading();
													uni.showToast({
														title: '手机号不能为空',
														icon:'error',
														duration: 2000
													});
												} else if (!reg.test(userTel)) {
													uni.hideLoading();
													uni.showToast({
														title: '手机号输入格式有误',
														icon:'error',
														duration: 2000
													});
												}else{
													uni.hideLoading();
													// console.log(userTel,"000")
													uni.request({
														url: 'http://localhost:8000/DriverPhoneNum',
														data: {
															phoneNum:userTel,
															openid:openid,
														},
														success: function(res) {
															// console.log(res.data, 95);
															if(res.data.result == 0){
																uni.showModal({
																    title: '提示',
																	content: '该手机号未注册，请前往进行注册',
																	confirmText: "立即前往", // 确认按钮文字  
																	showCancel: false,
																	success: (res) => {
																		if(res.confirm) {  
																			uni.navigateTo({
																			    url: '../DriverRegister/DriverRegister'
																			})
																		}
																	}
																})
															}else{
																if(res.data.data[0].auditState == 11){
																	// console.log(res.data, 107);
																	uni.setStorageSync("openid", )
																	// console.log('司机账户已审核，登录成功')
																	uni.showLoading({
																		title: '登录成功……'
																	});
																	setTimeout(function () {
																		let DriveruserTable={
																			userId:res.data.data[0].driverId,
																			userTel:res.data.data[0].phoneNumber,
																			driverName:res.data.data[0].driverName,
																			driverPhoto:res.data.data[0].driverPhoto,
																			CreditPoints:res.data.data[0].CreditPoints,
																			driverName:res.data.data[0].driverName,
																		}
																		uni.setStorageSync("DriveruserTable", DriveruserTable)
																		uni.hideLoading();
																		// 跳转链接放在这
																		uni.navigateTo({																																						url:"/pages/homePage/homePage"																																	})
																	}, 2000);
																}else if(res.data.data[0].auditState == 12){
																	that.alert=true
																	that.description = res.data.data[0].auditRemark
																	// console.log(111111)
																	// console.log('司机账户未审核通过，登录失败')
																	uni.showModal({
																	    title: '提示',
																		content: '该账户未审核通过，登录失败',
																		confirmText: "确定", // 确认按钮文字  
																		showCancel: false,
																	})
																}else{
																	// console.log('司机账户正在审核中，登录失败')
																	uni.showModal({
																	    title: '提示',
																		content: '该账户正在审核中，登录失败',
																		confirmText: "确定", // 确认按钮文字  
																		showCancel: false,
																	})
																}
																
															}
														}
													});
												}
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
								}else{
									uni.showLoading({
										title: '正在跳转……'
									});
									setTimeout(function () {
										uni.hideLoading();
										// console.log("跳转到打车页面")
										// console.log(res.data, 167);
										let DriveruserTable={
											userId:res.data.data[0].driverId,
											userTel:res.data.data[0].phoneNumber,
											driverName:res.data.data[0].driverName,
											driverPhoto:res.data.data[0].driverPhoto,
											CreditPoints:res.data.data[0].CreditPoints,
											driverName:res.data.data[0].driverName,
										}
										uni.setStorageSync("DriveruserTable", DriveruserTable)
										uni.setStorageSync("openid", )
										uni.navigateTo({																														url:"/pages/homePage/homePage"																								})
									}, 2000);
									
								}
							}
						});
   						
   					},
   					fail: (res) => {
						this.button=0
   						// console.log(res)
   					}
   				});
   			}
   		}
   	}
   </script>
 
<style>
 
    .content {
    	display: flex;
    	flex-direction: column;
    	align-items: center;
    	justify-content: center;
    }
    
    .logo {
    	height: 200rpx;
    	width: 200rpx;
    	margin-top: 200rpx;
    	margin-left: auto;
    	margin-right: auto;
    	margin-bottom: 50rpx;
    }
    
    .text-area {
    	display: flex;
    	justify-content: center;
    }
    
    .title {
    	font-size: 36rpx;
    	color: #8f8f94;
    }
    .btn {
		width: 90%;
    	padding: 0 100rpx;
		margin-top: 100rpx;
    }
	
</style>

